import config from "../../config/index.js";
import store from "../../store/index.js";
import tool from "../tool/index.js";

const theme = {
    // 显示问候语
    showGreatBox: () => {
        const greatMessageList = [
            {text: "晚安😴", start: 0, end: 5},
            {text: "早上好鸭👋, 祝你一天好心情！", start: 6, end: 9},
            {text: "上午好👋, 状态很好，鼓励一下～", start: 10, end: 10},
            {text: "11点多啦, 在坚持一下就吃饭啦～", start: 11, end: 11},
            {text: "午安👋, 宝贝", start: 12, end: 14},
            {text: "🌈充实的一天辛苦啦！", start: 14, end: 18},
            {text: "19点喽, 奖励一顿丰盛的大餐吧🍔。", start: 19, end: 19},
            {text: "晚上好👋, 在属于自己的时间好好放松😌~", start: 20, end: 24}
        ];
        let message = "";
        const now = new Date().getHours();
        for (let greatMessage of greatMessageList) {
            if (now >= greatMessage.start && now <= greatMessage.end) {
                message = greatMessage.text;
                break;
            }
        }
        store.commit("SET_GREAT_BOX_SHOW", true);
        return message;
    },

    // 隐藏问候语
    hideGreatBox: () => {
        store.commit("SET_GREAT_BOX_SHOW", false)
    },

    // 显示加载动画
    showLoadBox: () => {
        store.commit("SET_LOAD_BOX_LOADED", false)
    },

    // 隐藏加载动画
    hideLoadBox: () => {
        store.commit("SET_LOAD_BOX_LOADED", true)
    },

    // 显示控制台
    showConsole: () => {
        store.commit("SET_CONSOLE_IS_SHOW", true)
    },

    // 隐藏控制台
    hideConsole: () => {
        store.commit("SET_CONSOLE_IS_SHOW", false)
    },

    // 显示右键菜单
    showRightMenu: (isTrue, x = 0, y = 0) => {
        if (isTrue) {
            theme.stopMaskScroll();
            store.commit("SET_SITE_RIGHT_MENU_XPX", x);
            store.commit("SET_SITE_RIGHT_MENU_YPX", y);
            store.commit("SET_SITE_RIGHT_MENU_SHOW", true);
        } else {
            store.commit("SET_SITE_RIGHT_MENU_SHOW", false);
        }
    },

    // 隐藏右键菜单
    hideRightMenu: () => {
        theme.showRightMenu(false);
    },

    stopMaskScroll: () => {
        let xScroll = document.getElementById("rightmenu-mask");
        if (xScroll) {
            xScroll.addEventListener("mousewheel", function (e) {
                theme.hideRightMenu();
            }, {passive: true});
        }
        xScroll = document.getElementById("rightMenu");
        if (xScroll) {
            xScroll.addEventListener("mousewheel", function (e) {
                theme.hideRightMenu();
            }, {passive: true});
        }
    },

    // 快捷键开关
    keyboardToggle: () => {
        if (store.getters.getSiteKeyBoardToggle) {
            tool.snackbarShow("你已禁用快捷键", false, 2000);
            store.commit("SET_SITE_KEY_BOARD_TOGGLE", false);
        } else {
            tool.snackbarShow("你已启用快捷键", false, 2000);
            store.commit("SET_SITE_KEY_BOARD_TOGGLE", true);
        }
    },

    // 边栏控制
    switchShowAside: () => {
        document.documentElement.classList.toggle('hide-aside')
        if (store.getters.getSiteHideAside) {
            tool.snackbarShow("你已显示侧边栏", false, 2000);
            store.commit("SET_SITE_HIDE_ASIDE", false);
        } else {
            tool.snackbarShow("你已隐藏侧边栏", false, 2000);
            store.commit("SET_SITE_HIDE_ASIDE", true);
        }
    },

    // 热评控制
    switchCommentBarrage: () => {
        if (store.getters.getCommentBarrage) {
            tool.snackbarShow("你已禁用弹幕", false, 2000);
            store.commit("SET_SITE_COMMENT_BARRAGE", false)
        } else {
            tool.snackbarShow("你已启用弹幕", false, 2000);
            store.commit("SET_SITE_COMMENT_BARRAGE", true);
        }
    },

    showCookie: () => {
        theme.showCookieOnClass();
        theme.showCookieOnStyle();
    },

    showCookieOnClass: () => {
        store.commit("SET_SITE_COOKIES_WINDOW_CLASS_SHOW", true);
    },

    showCookieOnStyle: () => {
        store.commit("SET_SITE_COOKIES_WINDOW_STYLE_SHOW", true);
    },

    hideCookie: function () {
        setTimeout(() => {
            theme.hideCookieOnClass();
            setTimeout(() => {
                theme.hideCookieOnStyle();
            }, 1000);
        }, 3000);
    },

    hideCookieOnClass: () => {
        store.commit("SET_SITE_COOKIES_WINDOW_CLASS_SHOW", false);
    },

    hideCookieOnStyle: () => {
        store.commit("SET_SITE_COOKIES_WINDOW_STYLE_SHOW", false);
    },

    showNavToTop: () => {
        store.commit("SET_NAV_TO_TOP_SHOW", true);
    },

    hideNavToTop: () => {
        store.commit("SET_NAV_TO_TOP_SHOW", false);
    },

    toggleMenuClickTrue: () => {
        store.commit("SET_TOGGLE_MENU_CLICK", true);
    },

    toggleMenuClickFalse: () => {
        store.commit("SET_TOGGLE_MENU_CLICK", false);
    },

    /**
     * 使滚动条滚动到指定位置
     * @param e 参数一
     * @param t 参数二
     * @returns {*} 结果
     */
    scrollToDest: (e, t) => {
        if (e < 0 || t < 0)
            return;
        const currentScrollPosition = window.scrollY || window.screenTop;
        if (e >= 70) {
            e -= 70;
        }
        if ("CSS" in window && CSS.supports("scroll-behavior", "smooth")) {
            return void window.scrollTo({
                top: e,
                behavior: "smooth"
            });
        }
        let startTime = null;
        t = t || 500;
        window.requestAnimationFrame((function animateScroll(timestamp) {
            startTime = startTime || timestamp;
            if (currentScrollPosition < e) {
                const elapsedTime = timestamp - startTime;
                window.scrollTo(0, (e - currentScrollPosition) * elapsedTime / t + currentScrollPosition);
                if (elapsedTime < t) {
                    window.requestAnimationFrame(animateScroll);
                } else {
                    window.scrollTo(0, e);
                }
            } else {
                const elapsedTime = timestamp - startTime;
                window.scrollTo(0, currentScrollPosition - (currentScrollPosition - e) * elapsedTime / t);
                if (elapsedTime < t) {
                    window.requestAnimationFrame(animateScroll);
                } else {
                    window.scrollTo(0, e);
                }
            }
        }));
    },

    // 初始化主题颜色
    initThemeColor: () => {
        if (store.getters.getIsIndexPage) {
            const currentTop = window.scrollY || document.documentElement.scrollTop
            if (currentTop === 0) {
                let themeColor = getComputedStyle(document.documentElement).getPropertyValue('--heo-main');
                theme.changeThemeColor(themeColor);
            } else {
                let themeColor = getComputedStyle(document.documentElement).getPropertyValue('--heo-background');
                theme.changeThemeColor(themeColor);
            }
        } else {
            let themeColor = getComputedStyle(document.documentElement).getPropertyValue('--heo-background');
            theme.changeThemeColor(themeColor);
        }
    },

    // 改变主题颜色
    changeThemeColor: (color) => {
        if (document.querySelector('meta[name="theme-color"]') !== null) {
            document.querySelector('meta[name="theme-color"]').setAttribute('content', color)
        }
    },

    // 设置黑夜模式
    setDarkMode: () => {
        store.commit("SET_SITE_THEME_MODE", "dark")
        document.documentElement.setAttribute('theme_mode', 'dark');
        document.documentElement.classList.add('color-scheme-dark');
        theme.initThemeColor();
    },

    // 设置白天模式
    setLightMode: () => {
        store.commit("SET_SITE_THEME_MODE", "light")
        document.documentElement.setAttribute('theme_mode', 'light');
        document.documentElement.classList.remove('color-scheme-dark');
        theme.initThemeColor();
    },

    // 自动调节主题模式
    switchThemeMode: () => {
        if ("dark" === store.getters.getSiteThemeMode) {
            theme.setLightMode();
            tool.snackbarShow("你已切换为浅色模式", false, 2000);
        } else {
            theme.setDarkMode();
            tool.snackbarShow("你已切换为深色模式", false, 2000);
        }
        // 代码块
        if (config.prism.enable) {
            theme.dataCodeTheme();
        }
    },

    // 第三方代码显示控件主题样式
    dataCodeTheme: () => {
        const siteThemeMode = store.getters.getSiteThemeMode;
        const lightThemeLink = document.querySelector("link[data-code-theme=light]");
        const darkThemeLink = document.querySelector("link[data-code-theme=dark]");
        if (lightThemeLink || darkThemeLink) {
            if ("light" === siteThemeMode) {
                if (darkThemeLink) darkThemeLink.disabled = true;
                if (lightThemeLink) lightThemeLink.disabled = false;
            } else {
                if (lightThemeLink) lightThemeLink.disabled = true;
                if (darkThemeLink) darkThemeLink.disabled = false;
            }
        }
    },

    // 移除赞赏蒙版
    removeRewardMask: () => {
        if (!document.querySelector(".reward-main")) return;
        document.querySelector(".reward-main").style.display = "none";
        document.getElementById("quit-box").style.display = "none";
    },

}

export default theme;